// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let {OPENID}=cloud.getWXContext();
  let word = '该用户已被记录入client集合';
  var date=new Date();
  return cloud.database().collection("client")
  .where({openid:OPENID}) 
   .get()
   .then(res=>{
     if(res.data.length==0){
      cloud.database().collection("client")
      .add({
          data:{
            openid:OPENID,
            headshot:event.user.avatarUrl,
            nickname:event.user.nickName,
            credit:50,
            lotteryTimes:0,
            lotteryRecord:[],
            creditRecord:[{
              gettime: date.getFullYear()+'年'+(date.getMonth()+1)+'月'+date.getDate()+'日',
              quantity:50,
              reason:"每日广场页获取"
            }],
            time:+new Date()
          }
      })
     }
     else return word;
  })
  
}